package cn.lh.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.text.SimpleDateFormat;
import java.util.TimeZone;


@Configuration
public class JsonConfig {
 
    @Value("${spring.jackson.date-format}")
    private String dataFormat;
 
    @Value("${spring.jackson.time-zone}")
    private String timeZone;
 
    @Bean
    public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = converter.getObjectMapper();
        /*添加null值设置*/
//        mapper.setSerializerFactory(mapper.getSerializerFactory().withSerializerModifier(new NullCustomConverter.MyBeanSerializerModifier()));
        /*日期处理，自定义后一些配置文件里面的信息没有生效，我们手动操作*/
        mapper.setDateFormat(new SimpleDateFormat(dataFormat));
        /*时区处理*/
        mapper.setTimeZone(TimeZone.getTimeZone(timeZone));
        return converter;
    }
 
}
